<?php
/**
 * @page dynamic_pages Dynamic Pages
 * 
 * Dynamic pages allow you to create pages per paramater/field value. To explain it, lets consider you have a
 * custom field of dropdown type in your listing called "city" or "reigon", and now you want to create
 * a page which will show listings/reviews specific to that city or reigon only, that's where the 
 * concept of Dynamic pages comes. If you have 10 different options for "city" in dropdown, 
 * then 10 different city pages will be auto-generated. Its a must-have feature if you are building a website
 * focusing on a specific area.
 * 
 * Note: Its important to remember that dynamic pages are created only on fields of dropdown, radio, multiple select
 * multiple checkbox types.
 * 
 * Creating a dynamic page will also alter how some module shows content, for example you might have a 
 * category list on pages which shows up like this:
 * 
 * @image html module_on_all_pages.jpg
 * 
 * On the dynamic show it will show the information of that selected city only (see both images shows different
 * count of listings in a category):
 * 
 * @image html module_on_dynamic_page.jpg
 * 
 * @section dynamic_page_insert_module Populating dynamic pages with content
 * 
 * Once dynamic page is created from backend, you will need to insert modules inside it as per your preference.
 * All the content inside modules will be filtered as per the selected dynamic page. 
 * 
 * Whatever page you create, will also be added in "Visibility" options of Module add/edit page. 
 * 
 */
	class DynamicPagesController extends AppController
	{
		function index()
		{
			$pageId = $this->DynamicPage->getPageId(cr("DynamicPage.field"));
			$this->actionId = $pageId;
		}
		
		function admin_add()
		{
			$this->pageTitle = __('Add Dynamic Page');
			if($this->data)
			{
				if($this->DynamicPage->save($this->data))
				{
					$DynamicPage = new DynamicPageRoutes();
					$DynamicPage->savePagesPath($this->DynamicPage); 
					$this->successMsg(__("Page has been added."));
					$this->redirect(array("action"=>"index"));
				}else
				{
					$this->errorMsg(__("There are some errors. Please fix them."));
				}
			}
			$DynamicPage = new DynamicPageRoutes();
			$DynamicPage->savePagesPath($this->DynamicPage);
			$this->set("fields",$this->DynamicPage->getFieldsList());
		}
		
		function admin_index() {
			$this->pageTitle = __('Dynamic Pages');
			$this->DynamicPage->recursive = 0;
			$this->set('dynamicPages', $this->paginate());
			$this->helpers[] = "Index";
		}
		
		function admin_edit($id)
		{
			$dynamicPage = $this->DynamicPage->findById($id);
			if(!$dynamicPage)
			{
				$this->redirect(array("action"=>"index"));
			}
			if($this->data)
			{
				$this->data['DynamicPage']['id'] = $id;
				if($this->DynamicPage->save($this->data))
				{
					$DynamicPage = new DynamicPageRoutes();
					$DynamicPage->savePagesPath($this->DynamicPage);
					$this->successMsg(__("Page has been saved."));
					$this->redirect(array("action"=>"index"));
				}else
				{
					$this->errorMsg(__("There are some errors. Please fix them."));
				}
			}else
			{
				$this->data = $dynamicPage;
			}
			$this->set("fields",$this->DynamicPage->getFieldsList());
			$this->render("admin_add");
		}
		
	function admin_delete($id = null) 
	{
			if (!$id) 
			{
				$this->Session->setFlash(__('Invalid id for DynamicPage', true));
				$this->redirect(array('action'=>'index'));
			}
			if ($this->DynamicPage->del($id)) 
			{
				$DynamicPage = new DynamicPageRoutes();
				$DynamicPage->savePagesPath($this->DynamicPage);
				$this->successMsg(__('Dynamic Page deleted', true));
				$this->redirect(array('action'=>'index'));
			}
	}
		
}
?>